/*
* Copyright (c) 2010-2011 Nokia Corporation and/or its subsidiary(-ies).
* All rights reserved.
* This component and the accompanying materials are made available
* under the terms of "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
* Initial Contributors:
* Nokia Corporation - initial contribution.
*
* Contributors:
*
* Description: Viewfinder container.
*
*/

// INCLUDE FILES
#include <math.h>
#include <gdi.h> // CFont
#include <AknDef.h>
#include "cameralitytraces.h"
#include "cameralityappui.h"
#include "cameralityviewfindercontainer.h"

// ================= MEMBER FUNCTIONS =======================

/*!
* Two-phased constructor.
* @return Container instance.
*/
CCameralityViewfinderContainer* CCameralityViewfinderContainer::NewL()
    {
    CCameralityViewfinderContainer* self 
        = new (ELeave) CCameralityViewfinderContainer();
    CleanupStack::PushL(self);
    self->ConstructL();
    CleanupStack::Pop(self);
    return self;
    }

/*!
* Constructor.
*/
CCameralityViewfinderContainer::CCameralityViewfinderContainer()
    : iError(KErrNone)      
    {
    }


/*!
* Second phase constructor.
*/
void CCameralityViewfinderContainer::ConstructL()
    {
    TRACE_ENTER();
    DoConstructL();

    // Create the viewfinder control
    iViewfinder = CCameralityViewfinder::NewL();
    iViewfinder->SetObserver(this);
    TRACE_EXIT();
    }

/*!
* Destructor.
* Releases any used resources.
*/
CCameralityViewfinderContainer::~CCameralityViewfinderContainer()
    {
    TRACE_ENTER();
    if (iViewfinder) 
        {
        iViewfinder->SetObserver(NULL);
        iViewfinder->Release();
        delete iViewfinder;
        }
    TRACE_EXIT();
    }

/*!
* Called by framework when the view size is changed.
*/
void CCameralityViewfinderContainer::SizeChanged()
    {
    TRACE_ENTER();
    if (iViewfinder->State() == CCameralityViewfinder::ERunning)
        {
        TRACE("[Camerality] Restarting viewfinder");
        TRect rect(Rect());
        iViewfinder->Start(Window(), rect);
        }
    DrawNow();
    TRACE_EXIT();
    }

/*!
* Normal CCoeControl drawing method.
*/
void CCameralityViewfinderContainer::Draw(const TRect& /*aRect*/) const
    {
    TRACE_ENTER();
    CWindowGc& gc = SystemGc();

    if (iError == KErrNone) {
        // Draw transparent content to see viewfinder through.
        gc.SetPenStyle(CGraphicsContext::ENullPen);
        gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
        gc.SetDrawMode(CGraphicsContext::EDrawModeWriteAlpha);
        gc.SetBrushColor(TRgb::Color16MA(0));
        gc.DrawRect(Rect());
        
    } else {
        // Draw solid background if error is encountered.
        gc.SetPenStyle(CGraphicsContext::ENullPen);
        gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
        gc.SetBrushColor(KRgbBlack);
        gc.DrawRect(Rect());

        // Draw text showing the error code.
        const CFont* font = CEikonEnv::Static()->NormalFont();
        TBuf<255> buffer;         
        _LIT(KErrorTxt, "Camera error: %d");
        buffer.Format(KErrorTxt, iError);

        gc.SetBrushStyle(CGraphicsContext::ENullBrush);
        gc.SetPenStyle(CGraphicsContext::ESolidPen);
        gc.SetPenColor(KRgbWhite);
        gc.UseFont(font);
        gc.DrawText(buffer, TPoint(0, 20));
    }

    TRACE_EXIT();
    }

/*!
* Handle change in viewfinder state.
*/
void CCameralityViewfinderContainer::ViewfinderStateChanged(
        CCameralityViewfinder::TViewfinderState /*oldState*/,
        CCameralityViewfinder::TViewfinderState /*newState*/)
    {
    // No action here, could do something after viewfinder started.
    }

/*!
* Handle error in viewfinder.
* Viewfinder releases camera in case of any error.
*/
void CCameralityViewfinderContainer::ViewfinderError(TInt aError)
    {
    TRACE_ENTER();
    iError = aError;
    DrawDeferred();
    TRACE_EXIT();
    }

/*!
* Show this control.
* @param aExtent Extent of this container.
*/
void CCameralityViewfinderContainer::Show(const TRect& aExtent)
    {
    TRACE_ENTER();
    iError = KErrNone;
    CCameralityBaseContainer::Show(aExtent);
    TRACE("[Camerality] Initializing viewfinder..");
    TRect rect(Rect());
    iViewfinder->Start(Window(), rect);
    TRACE_EXIT();
    }

/*!
* Hide this control.
*/
void CCameralityViewfinderContainer::Hide()
    {
    iViewfinder->Release();    
    iError = KErrNone;
    CCameralityBaseContainer::Hide();
    }

// End of File
